package net.kazed.nextaction.database;

import net.kazed.android.database.Table;
import net.kazed.nextaction.database.Task.Column;
import android.content.ContentValues;
import android.database.Cursor;

/**
 * Definition of Task with join to TaskContext table.
 */
public class TaskJoinTable extends Table<Task> {
    
    public static final String TABLE = "task";

    /**
     * Constructor.
     */
    public TaskJoinTable() {
        super(TABLE, Task.Column.DESCRIPTION);
        addColumn(Task.Column.ID);
        addColumn(Task.Column.DESCRIPTION);
        addColumn(Task.Column.DETAILS);
        addColumn(Task.Column.CONTEXT_ID);
        addColumn(Task.Column.PARENT_TASK_ID);
        addColumn(Task.Column.CREATED_DATE);
        addColumn(Task.Column.MODIFIED_DATE);
        addColumn(Task.Column.START_DATE);
        addColumn(Task.Column.DUE_DATE);
        addColumn(Task.Column.DISPLAY_ORDER);
        addColumn(Task.Column.COMPLETE);
        addColumn(Task.Column.TYPE);
        addColumn(Task.Column.STATUS);
        addColumn(Task.Column.NUMBER_OF_SUB_TASKS);
        addColumn(Task.Column.NUMBER_OF_ALL_SUB_TASKS);
    }
    
    /**
     * Extract object from cursor.
     * @param cursor Query cursor.
     * @return Extracted object.
     */
    public Task extract(final Cursor cursor) {
        final Task task = new Task(cursor);
        return task;
    }

    /**
     * Insert fragment values into collection.
     * @param fragment Model object.
     * @return Content values.
     */
    public ContentValues getValues(final Task task) {
        final ContentValues values = new ContentValues();
        values.put(Task.Column.ID, task.getId());
        values.put(Task.Column.DESCRIPTION, task.getDescription());
        values.put(Task.Column.DETAILS, task.getDetails());
        values.put(Task.Column.CONTEXT_ID, task.getContextId());
        if (task.getParentTaskId() == null) {
           values.putNull(Column.PARENT_TASK_ID);
        } else {
           values.put(Task.Column.PARENT_TASK_ID, task.getParentTaskId());
        }
        if (task.getCreated() != null) {
           values.put(Task.Column.CREATED_DATE, task.getCreated().getTime());
        }
        if (task.getModified() != null) {
           values.put(Task.Column.MODIFIED_DATE, task.getModified().getTime());
        }
        if (task.getStartDate() == null) {
           values.put(Task.Column.START_DATE, task.getStartDate().getTime());
        }
        if (task.getDueDate() == null) {
           values.put(Task.Column.DUE_DATE, task.getDueDate().getTime());
        }
        values.put(Task.Column.DISPLAY_ORDER, task.getOrder());
        if (task.getComplete()) {
           values.put(Task.Column.COMPLETE, 1);
        } else {
           values.putNull(Column.COMPLETE);
        }
        if (task.getStatus() != null) {
           values.put(Task.Column.STATUS, task.getStatus().getType());
        }
        if (task.getNumberOfSubTasks() == null) {
           values.put(Task.Column.NUMBER_OF_SUB_TASKS, task.getNumberOfSubTasks());
        }
        if (task.getNumberOfAllSubTasks() == null) {
           values.put(Task.Column.NUMBER_OF_ALL_SUB_TASKS, task.getNumberOfAllSubTasks());
        }
        return values;
    }

}
